package com.hzhr.iot.device.sdk.service;

import com.hzhr.iot.core.constant.Common_ErrorsEnum;
import com.hzhr.iot.core.dto.ResponseExceptionDto;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrHttpUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.device.sdk.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.sdk.model.regulatorDevice_monitor_collectData.RegulatorDevice_Monitor_CollectDataGetModel;
import com.hzhr.iot.device.sdk.model.regulatorDevice_monitor_collectData.RegulatorDevice_Monitor_CollectDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;

@Service
public class Device_RegulatorDevice_Monitor_CollectDataService {
    @Autowired
    RestTemplate restTemplate;

    /**
     * 查询调压设备下的监控设备 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询调压设备 : <br/>
     * 查询COLLECT_DEVICE对象regulator_1， <br/>
     * 查询规则如下： <br/>
     *           (1) COLLECT_DEVICE.ID == 路径参数regulatorDeviceId; <br/>
     *           (2) COLLECT_DEVICE.IS_VALID == '1'； <br/>
     * 如果regulator_1不存在，抛出异常201； <br/>
     * 2. 查询调压设备的下的监控设备 : <br/>
     * 查询IOT_DEVICE对象monitor_1, <br/>
     * 查询规则如下： <br/>
     *           (1) IOT_DEVICE.COLLECT_DEVICE_ID == regulator_1.ID; <br/>
     *           (2) IOT_DEVICE.ID = 路径参数monitorId; <br/>
     *           (3) IOT_DEVICE.IS_VALID == '1'; <br/>
     * 3. 查询监控设备的采集数据(根据采集时间降序排列) <br/>
     * 查询REGULATOR_COLLECT_DATA对象集合collectDatas_1及总数， <br/>
     * 查询规则如下： <br/>
     *           (1) 条件参数的筛选条件; <br/>
     *           (2) COLLECT_DATA.IOT_DEVICE_ID = monitor_1.ID; <br/>
     *           (3) COLLECT_DATA.IS_VALID = '1'; <br/>
     * 3. 返回信息 <br/>
     * 根据条件参数、collectDatas_1及总数生成RegulatorDevice_Monitor_CollectDataGetModel对象result并返回， <br/>
     * RegulatorDevice_Monitor_CollectDataModel与REGULATOR_COLLECT_DATA的对应关系如下： <br/>
     *           (1) RegulatorDevice_Monitor_CollectDataModel.inPressure => REGULATOR_COLLECT_DATA.IN_PRESSURE; <br/>
     *           (2) RegulatorDevice_Monitor_CollectDataModel.envTemperature => REGULATOR_COLLECT_DATA.ENV_TEMPERATURE; <br/>
     *           (3) RegulatorDevice_Monitor_CollectDataModel.outPressure1 => REGULATOR_COLLECT_DATA.OUT_PRESSURE1; <br/>
     *           (4) RegulatorDevice_Monitor_CollectDataModel.outPressure2 => REGULATOR_COLLECT_DATA.OUT_PRESSURE2; <br/>
     *           (5) RegulatorDevice_Monitor_CollectDataModel.outPressure3 => REGULATOR_COLLECT_DATA.OUT_PRESSURE3; <br/>
     *           (6) RegulatorDevice_Monitor_CollectDataModel.outPressure4 => REGULATOR_COLLECT_DATA.OUT_PRESSURE4; <br/>
     *           (7) RegulatorDevice_Monitor_CollectDataModel.outTemperature1 => REGULATOR_COLLECT_DATA.OUT_TEMPERATURE1; <br/>
     *           (8) RegulatorDevice_Monitor_CollectDataModel.outTemperature2 => REGULATOR_COLLECT_DATA.OUT_TEMPERATURE2; <br/>
     *           (9) RegulatorDevice_Monitor_CollectDataModel.outTemperature3 => REGULATOR_COLLECT_DATA.OUT_TEMPERATURE3; <br/>
     *           (10) RegulatorDevice_Monitor_CollectDataModel.outTemperature4 => REGULATOR_COLLECT_DATA.OUT_TEMPERATURE4; <br/>
     *           (11) RegulatorDevice_Monitor_CollectDataModel.outStaTotalFlow1 => REGULATOR_COLLECT_DATA.OUT_STA_TOTAL_FLOW1; <br/>
     *           (12) RegulatorDevice_Monitor_CollectDataModel.outStaTotalFlow2 => REGULATOR_COLLECT_DATA.OUT_STA_TOTAL_FLOW2; <br/>
     *           (13) RegulatorDevice_Monitor_CollectDataModel.outStaTotalFlow3 => REGULATOR_COLLECT_DATA.OUT_STA_TOTAL_FLOW3; <br/>
     *           (14) RegulatorDevice_Monitor_CollectDataModel.outStaTotalFlow4 => REGULATOR_COLLECT_DATA.OUT_STA_TOTAL_FLOW4; <br/>
     *           (15) RegulatorDevice_Monitor_CollectDataModel.outStaFlowRate1 => REGULATOR_COLLECT_DATA.OUT_STA_FLOW_RATE1; <br/>
     *           (16) RegulatorDevice_Monitor_CollectDataModel.outStaFlowRate2 => REGULATOR_COLLECT_DATA.OUT_STA_FLOW_RATE2; <br/>
     *           (17) RegulatorDevice_Monitor_CollectDataModel.outStaFlowRate3 => REGULATOR_COLLECT_DATA.OUT_STA_FLOW_RATE3; <br/>
     *           (18) RegulatorDevice_Monitor_CollectDataModel.outStaFlowRate4 => REGULATOR_COLLECT_DATA.OUT_STA_FLOW_RATE4; <br/>
     *           (19) RegulatorDevice_Monitor_CollectDataModel.collectTime => REGULATOR_COLLECT_DATA.COLLECT_TIME; <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where REGULATOR_COLLECT_DATA.COLLECT_TIME >= #{_greaterthanorequal_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthan_collectTime : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where REGULATOR_COLLECT_DATA.COLLECT_TIME < #{_lessthan_collectTime};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/><br/>
     * @param regulatorDeviceId 调压设备编号 <br/><br/>
     * @param monitorId 监控设备编号 <br/><br/>
     * @throws ServiceException <br/>
     * @see RegulatorDevice_Monitor_CollectDataGetModel result <br/>
     */ 
    public RegulatorDevice_Monitor_CollectDataGetModel get(Map<String, String> header, Integer regulatorDeviceId, Integer monitorId, Map<String, Object> query) throws ServiceException {
        HttpHeaders headers = new HttpHeaders();
        if (header == null) header = new HashMap<>();
        for (Map.Entry<String, String> entry:
        header.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        headers.add("Content-Type", "application/json;charset-utf-8");
        headers.add("accept", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(null, headers);

        Map<String, Object> uriVariables = new HashMap<>();
        uriVariables.put("regulatorDeviceId", regulatorDeviceId);
        uriVariables.put("monitorId", monitorId);

        try {
            ResponseEntity<RegulatorDevice_Monitor_CollectDataGetModel> result = restTemplate.exchange("http://device-service/regulator-devices/{regulatorDeviceId}/monitors/{monitorId}/collect-datas?" + HzhrHttpUtil.queryMap2url(query), HttpMethod.GET, httpEntity, RegulatorDevice_Monitor_CollectDataGetModel.class, uriVariables);
            return result.getBody();
        } catch (HttpClientErrorException e) {
            e.printStackTrace();
            if (e.getStatusCode().equals(HttpStatus.UNAUTHORIZED)) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_2"));
            }
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
            } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("REGULATORDEVICE_MONITOR_COLLECTDATASERVICE_GET_" + code));
            }
        } catch (HttpServerErrorException e) {
            e.printStackTrace();
            ResponseExceptionDto response = HzhrJsonUtil.json2object(e.getResponseBodyAsString(), ResponseExceptionDto.class);
            Integer code = response.getCode();
            if (code <= 100) {
                throw new ServiceException(Common_ErrorsEnum.getEnumByName("COMMON_" + code));
             } else {
                 throw new ServiceException(Device_ErrorsEnum.getEnumByName("REGULATORDEVICE_MONITOR_COLLECTDATASERVICE_GET_" + code));
             }
        } catch (Throwable e) {
              e.printStackTrace();
              throw e;
        }
    }
}

